<?php

namespace App\Kuafu\Pay\Controller\Merchant;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Order\Enum\OrderMerchantEnum;
use App\Kuafu\Order\Exceptions\OrderException;
use App\Kuafu\Order\Service\OrderMerchantUseService;
use App\Kuafu\Pay\Service\PayService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class PayController extends BaseController
{
    /**
     * 支付明细统计 - 列表
     * @param Request $request
     * @return JsonResponse
     * @throws OrderException
     */
    public function lists(Request $request)
    {
        $showTimeTab = (int)$request->get('showTimeTab', OrderMerchantEnum::SHOW_TIME_TAB_TODAY);
        $showStartAt = (int)$request->get('showStartAt', 0);
        $showEndAt   = (int)$request->get('showEndAt', 0);
        $payType     = (int)$request->get('payType');
        $payStatus   = (int)$request->get('payStatus');
        $lastUnionId = (int)$request->get('lastUnionId', 0);
        $limit       = (int)$request->get('limit', 20);

        $data = (new OrderMerchantUseService([$this->shopId]))
            ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
            ->payUnionLists($payType, $payStatus, $lastUnionId, $limit);

        return $this->success(['lists' => $data]);
    }

    /**
     * 支付明细统计 - 聚合统计数据
     * @param Request $request
     * @return JsonResponse
     * @throws OrderException
     */
    public function listsStatistics(Request $request)
    {
        $showTimeTab = (int)$request->get('showTimeTab', OrderMerchantEnum::SHOW_TIME_TAB_TODAY);
        $showStartAt = (int)$request->get('showStartAt', 0);
        $showEndAt   = (int)$request->get('showEndAt', 0);
        $payType     = (int)$request->get('payType');
        $payStatus   = (int)$request->get('payStatus');

        $data = (new OrderMerchantUseService([$this->shopId]))
            ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
            ->payUnionListsStatistics($payType, $payStatus);

        return $this->success($data);
    }

    /**
     * 支付明细统计 - 筛选项枚举
     * @param Request $request
     * @return JsonResponse
     */
    public function listsFilter(Request $request)
    {
        return $this->success(OrderMerchantUseService::payUnionListsFilter());
    }

    /**
     * 支付明细统计 - 详情
     * @param Request $request
     * @return JsonResponse
     */
    public function detail(Request $request)
    {
        $unionId = (int)$request->get('unionId');

        $data = (new OrderMerchantUseService([$this->shopId]))
            ->payUnionDetail($unionId);
        return $this->success($data);
    }


    /**
     * 支付方式 - 聚合统计
     *
     * @param Request $request
     * @return JsonResponse
     * @throws OrderException
     */
    public function groupStatistics(Request $request): JsonResponse
    {
        $showTimeTab = (int)$request->get('showTimeTab', OrderMerchantEnum::SHOW_TIME_TAB_YESTERDAY);
        $showStartAt = (int)$request->get('showStartAt', 0);
        $showEndAt   = (int)$request->get('showEndAt', 0);

        return $this->success(
            (new OrderMerchantUseService([$this->shopId]))
                ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
                ->groupStatistics()
        );
    }

    /**
     * 支付明细统计 - 列表
     * @param Request $request
     * @return JsonResponse
     * @throws OrderException
     */
    public function listsNew(Request $request)
    {
        $showTimeTab = (int)$request->get('showTimeTab', OrderMerchantEnum::SHOW_TIME_TAB_TODAY);
        $showStartAt = (int)$request->get('showStartAt', 0);
        $showEndAt   = (int)$request->get('showEndAt', 0);
        $payType     = (int)$request->get('payType');
        $payStatus   = (int)$request->get('payStatus');
        $lastUnionId = (int)$request->get('lastUnionId', 0);
        $limit       = (int)$request->get('limit', 20);

        $data = (new OrderMerchantUseService([$this->shopId]))
            ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
            ->payUnionLists($payType, $payStatus, $lastUnionId, $limit, true);

        return $this->success(['lists' => $data]);
    }

    /**
     * 支付方式 - 聚合统计 2023-07-21
     *
     * @param Request $request
     * @return JsonResponse
     * @throws OrderException
     */
    public function groupStatisticsNew(Request $request): JsonResponse
    {
        $showTimeTab = (int)$request->get('showTimeTab', OrderMerchantEnum::SHOW_TIME_TAB_YESTERDAY);
        $showStartAt = (int)$request->get('showStartAt', 0);
        $showEndAt   = (int)$request->get('showEndAt', 0);

        return $this->success(
            (new OrderMerchantUseService([$this->shopId]))
                ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
                ->groupStatisticsNew()
        );
    }

    /**
     * 支付明细统计 - 聚合统计数据 2023-07-21
     * @param Request $request
     * @return JsonResponse
     * @throws OrderException
     */
    public function listsStatisticsNew(Request $request)
    {
        $showTimeTab = (int)$request->get('showTimeTab', OrderMerchantEnum::SHOW_TIME_TAB_TODAY);
        $showStartAt = (int)$request->get('showStartAt', 0);
        $showEndAt   = (int)$request->get('showEndAt', 0);
        $payType     = (int)$request->get('payType');
        $payStatus   = (int)$request->get('payStatus');

        $data = (new OrderMerchantUseService([$this->shopId]))
            ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
            ->payUnionListsStatisticsNew($payType, $payStatus);

        return $this->success($data);
    }
}
